package com.qf.ssm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.ssm.constans.ResultCode;
import com.qf.ssm.exceptions.DelDataException;
import com.qf.ssm.exceptions.FindDataException;
import com.qf.ssm.exceptions.MybaseException;
import com.qf.ssm.mapper.GoodsTypeMapper;
import com.qf.ssm.pojo.TbGoodsType;
import com.qf.ssm.service.GoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

@Service
@Transactional
public class GoodsTypeServiceImpl implements GoodsTypeService {
    private GoodsTypeMapper goodsTypeMapper;
    @Autowired
    public void setGoodsTypeMapper(GoodsTypeMapper goodsTypeMapper) {
        this.goodsTypeMapper = goodsTypeMapper;
    }

    @Override
    public TbGoodsType findById(Long id) {
        //断言，当前的判断条件一定符合内部的表达式要求，否则，就会执行后面的操作
        Assert.isTrue(id>0,()->{
            throw new FindDataException("id不符合要求", ResultCode.DATA_NOT_ALLOWED);
        });
        TbGoodsType goodsType = goodsTypeMapper.findById(id);
        Assert.isTrue(goodsType!=null,()->{
            throw new FindDataException("没有查询到该数据",ResultCode.ID_NOT_ALLOWED);
        });
        return goodsType;
    }

    @Override
    public void addGoodsType(TbGoodsType goodsType) {

        Assert.isTrue(!goodsType.isEmpty(),()->{
            throw new FindDataException("数据不完整",ResultCode.DATA_NOT_ALLOWED);
        });
        goodsTypeMapper.addGoodsType(goodsType);
    }

    @Override
    public PageInfo<TbGoodsType> findByPage(int pageSize, int pageNum) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        List<TbGoodsType> goodsTypes = goodsTypeMapper.findAll();
        PageInfo<TbGoodsType> PageInfo = new PageInfo<>(goodsTypes);
        return PageInfo;
    }

    @Override
    public void deleteById(Long id) {
        Assert.isTrue(id>0,()->{
            throw new DelDataException("id不符合规范",ResultCode.ID_NOT_ALLOWED);
        });
        int o = goodsTypeMapper.deleteById(id);
        Assert.isTrue(o>0,()->{
            throw new DelDataException("没有要删除的数据",ResultCode.DATA_NOT_EXIST);
        });
    }

}
